﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GeneralDFS.Common;
using GeneralDFS.DataEntity.Enum;

namespace GeneralDFS.DataEntity.Nodes
{
    [Serializable]
    public class NameNodeInfo:IComparable,IComparable<NameNodeInfo>
    {
        private NameNodeState _nameNodeState;

        /// <summary>
        /// 获取唯一标识符
        /// </summary>
        public UInt64 ID { get { return DFSUtils.GetHash(Ip + ":" + RpcPort); } }

        public String ServerName { set; get; }
        public String Ip { set; get; }
        public int RpcPort { set; get; }
        public int AgentRpcPort { set; get; }
        public long LastUpdateTime { set; get; }

        /// <summary>
        /// Set the state of the NameNode
        /// </summary>
        /// <param name="nameNodeState"></param>
        public void SetState(NameNodeState nameNodeState)
        {
            _nameNodeState = nameNodeState;
        }

        /// <summary>
        /// Get the NameNode State
        /// </summary>
        /// <returns></returns>
        public NameNodeState GetNodeState()
        {
            return _nameNodeState;
        }

        public int CompareTo(object obj)
        {
            var info = obj as NameNodeInfo;
            if (info != null)
            {
                var node = info;
                return this.ID.CompareTo(node.ID);
            }

            throw new ArgumentException("Object is not a NameNodeInfo.");
        }

        public int CompareTo(NameNodeInfo other)
        {
            return this.ID.CompareTo(other.ID);
        }

        public override bool Equals(object obj)
        {
            try
            {
                var node = (NameNodeInfo)obj;
                return this.ID == node.ID;
            }
            catch
            {
                // Equality operation should not throw...
                return false;
            }
        }

        /// <summary>
        /// Get hash code.
        /// </summary>
        /// <returns>Hash code.</returns>
        public override int GetHashCode()
        {
// ReSharper disable BaseObjectGetHashCodeCallInGetHashCode
            return base.GetHashCode();
// ReSharper restore BaseObjectGetHashCodeCallInGetHashCode
        }

        public override string ToString()
        {
            return string.Format("{0}:{1} ({2})", Ip, RpcPort, this.ID.ToString("x10").ToUpper());
        }
    }
}
